package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.take;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;

import java.util.ArrayList;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalTakeMedicineDetailBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalTakeMedicineDetailPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalTakeMedicineDetailViewModel;

public class InHospitalTakeMedicineDetailActivity extends BaseActivity<ActivityInHospitalTakeMedicineDetailBinding,InHospitalTakeMedicineDetailViewModel,InHospitalTakeMedicineDetailPresenter> {
    private  ArrayList<InHospitalPatientBeen> currently;

    private MDialog mDialog;
    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);


        binding.medicineDetailTitle.titleTextTv.setText("扫码取药");
        binding.medicineDetailTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.rvMedicineDetail.setLayoutManager(new LinearLayoutManager(this));
        binding.rvMedicineDetail.setPullRefreshEnabled(false);
        binding.medicineDetailTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != viewModel.datas && null != presenter.FitstData &&
                        viewModel.datas.size() < presenter.FitstData.size()){
                    mDialog = CreateDialog.exitDialog(InHospitalTakeMedicineDetailActivity.this, 5, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    },"退出取药单后，需重新开始操作");
                }else{
                    finish();
                }
            }
        });
        binding.medicineDetailTitle.titleBackTv.setText("");
        //开单号
        binding.tvMedicineDetailChufanghao.setText("NO: "+presenter.DJJLID);
        //开单科室
        binding.tvMedicineDetailPatientRoom.setText(presenter.FitstData.get(0).getRYBQMC());
        //开单时间
        binding.tvMedicineDetailTime.setText("开单时间: "+presenter.FitstData.get(0).getFYCZSJ());
        //扫码广播收到 准备跳转 传递数据
        binding.btMedicineDetailStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO:传递页面展现的药品集合过去
                Intent intent = new Intent(InHospitalTakeMedicineDetailActivity.this, InHospitalTakeProgressActivity.class);
                //处方号
                intent.putExtra("DJJLID",presenter.DJJLID);
                intent.putExtra("total", viewModel.datas);
                startActivityForResult(intent, Contants.REQUEST_CODE);
            }
        });
        //TODO:获取RecycleView的总条目数
        if(presenter.FitstData != null){
            binding.tvMedicineDetailTotalNum.setText("共"+ presenter.FitstData.size()+"种药品剩余");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                //TODO:用户未操作完全的药单 按BACK键返回  刷新界面数据
                currently = (ArrayList<InHospitalPatientBeen>) data.getExtras().getSerializable("currently");
//                Log.e("TAG", "onActivityResult: "+currently.size()+currently.toString() );
                if(null != currently && currently.size()<viewModel.datas.size()){
                    viewModel.setWData(currently);
                    binding.tvMedicineDetailTotalNum.setText("共"+ viewModel.datas.size()+"种药品剩余");
                }
                break;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(null != viewModel.datas && null != presenter.FitstData &&
                    viewModel.datas.size() < presenter.FitstData.size()){
                mDialog = CreateDialog.exitDialog(InHospitalTakeMedicineDetailActivity.this, 5, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                },"退出取药单后，需重新开始操作");
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != mDialog){
            mDialog.cancel();
        }
        PreferenceManager.getInstance().setHospitalTakeProgressTotal("0");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_take_medicine_detail;
    }

    @Override
    protected Class<InHospitalTakeMedicineDetailPresenter> getPresenterClass() {
        return InHospitalTakeMedicineDetailPresenter.class;
    }

    @Override
    protected Class<InHospitalTakeMedicineDetailViewModel> getViewModelClass() {
        return InHospitalTakeMedicineDetailViewModel.class;
    }
}
